<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>

    <link rel="stylesheet" hrel="http://localhost/4.17/esri/css/main.css" />
    <script src="http://localhost/4.17/dojo/dojo.js"></script>
    <!-- 引用在线arcgis api -->
    <style>
        html,
        body,
        #viewDiv {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
    </style>

    <script>
        require([
            "esri/Map",
            "esri/Basemap",
            "esri/views/MapView",
            "esri/views/SceneView",
            "esri/geometry/Extent",
            "esri/geometry/SpatialReference",
            //几何
            "esri/geometry/Point",
            "esri/geometry/Polyline",
            "esri/geometry/Polygon",
            "esri/Graphic",
            "esri/widgets/Sketch",
            "esri/Color",
            //工具
            "esri/views/draw/Draw",
            //切片地图信息
            "esri/layers/support/TileInfo",
            //图层
            "esri/layers/WebTileLayer",
            "esri/views/layers/LayerView",
            "esri/layers/TileLayer",
            "esri/layers/MapImageLayer",
            "esri/layers/FeatureLayer",
            "esri/layers/GraphicsLayer",
            //符号化
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/SimpleLineSymbol",
            //缓冲参数，用于GeometryService
            "esri/tasks/support/BufferParameters",
            //几何处理，投影、简化、缓冲区
            "esri/tasks/GeometryService",
            //几何引擎，用于测试、测量和分析两个或多个二维几何图形之间的空间关系,缓冲、修剪、距离、相交、联合、面积距离计算等。
            "esri/geometry/geometryEngine",
            //代理
            "esri/core/urlUtils",
            //查询
            "esri/tasks/FindTask",
            "esri/tasks/support/FindParameters",
            "esri/tasks/QueryTask",
            "esri/tasks/support/Query",
            "esri/tasks/IdentifyTask",
            "esri/tasks/support/IdentifyParameters",
            //打印地图
            "esri/tasks/PrintTask",
            "esri/tasks/support/PrintTemplate",
            "esri/tasks/support/PrintParameters",
            "dojo/dom",
            "dojo/on",
            "dojo/domReady!"
        ], function(
            Map,
            Basemap,
            MapView,
            SceneView,
            Extent,
            SpatialReference,
            Point,
            Polyline,
            Polygon,
            Graphic,
            Sketch,
            Color,
            Draw,
            TileInfo,
            WebTileLayer,
            LayerView,
            TileLayer,
            MapImageLayer,
            FeatureLayer,
            GraphicsLayer,
            SimpleMarkerSymbol,
            SimpleFillSymbol,
            SimpleLineSymbol,
            BufferParameters,
            GeometryService,
            geometryEngine,
            urlUtils,
            FindTask,
            FindParameters,
            QueryTask,
            Query,
            IdentifyTask,
            IdentifyParameters,
            PrintTask,
            PrintTemplate,
            PrintParameters,
            dom,
            on) {
            var map = new Map({ //属性：allLayers、ground、layers
                basemap: "topo",
            });
            var view = new MapView({
                container: "viewDiv",
                map: map,
                zoom: 4,
                center: [55, 65] // longitude, latitude
            });
            var draw = new Draw({
                view: view
            });

            $(".drawPolygon").on("click", function() {
                //createGon();
                //createPoint();
                createLine();
            });

            function createPoint() {
                //point、multipoint、polyline、polygon、rectangle, circle, ellipse
                var action = draw.create(
                    "point", {
                        mode: "click"
                    }
                );
                action.on("cursor-update", function(evt) {
                    createGonGraphic(evt.vertices);
                });
                action.on("draw-complete", function(evt) {
                    createGonGraphic(evt.vertices);
                });
            };

            function createGon() {
                var action = draw.create(
                    "polygon", {
                        mode: "click"
                    }
                );
                action.on("vertex-add", function(evt) {
                    createGonGraphic(evt.vertices);
                });
                action.on("vertex-remove", function(evt) {
                    createGonGraphic(evt.vertices);
                });
                action.on("cursor-update", function(evt) {
                    createGonGraphic(evt.vertices);
                });
                action.on("draw-complete", function(evt) {
                    createGonGraphic(evt.vertices);
                });
            };

            function createLine() {
                var action = draw.create(
                    "polyline", {
                        mode: "click"
                    }
                );
                var polyline = "polyline";
                action.on("vertex-add", function(evt) {
                    createGonGraphic(evt.vertices, polyline);
                });
                action.on("vertex-remove", function(evt) {
                    createGonGraphic(evt.vertices, polyline);
                });
                action.on("cursor-update", function(evt) {
                    createGonGraphic(evt.vertices, polyline);
                });
                action.on("draw-complete", function(evt) {
                    createGonGraphic(evt.vertices, polyline);
                });
            };

            //地图绘制面
            function createGonGraphic(vertices, geoType) {
                //view.graphics.removeAll();
                var index = vertices.length - 1;
                var markerSymbol = {
                    type: "simple-marker",
                    color: [226, 119, 40],
                    size: 8,
                    outline: {
                        color: [255, 255, 255],
                        width: 2
                    }
                };
                var linesymbol = {
                    type: "simple-line",
                    color: [255, 0, 0],
                    width: 2,
                    cap: "round",
                    join: "round"
                };
                var fillSymbol = {
                    type: "simple-fill",
                    color: [227, 139, 79, 0.8],
                    outline: {
                        color: [255, 255, 255],
                        width: 1
                    }
                };

                var point = {
                    type: "point",
                    x: vertices[index][0],
                    y: vertices[index][1],
                    spatialReference: view.spatialReference
                };
                var graphicPoint = new Graphic({
                    geometry: point,
                    symbol: markerSymbol
                });
                view.graphics.add(graphicPoint);

                var rings = [];
                for (let i = 0; i < vertices.length; i++) {
                    var point = {
                        type: "point",
                        x: vertices[i][0],
                        y: vertices[i][1],
                        spatialReference: view.spatialReference
                    };
                    var arr = [];
                    arr.push(vertices[i][0]);
                    arr.push(vertices[i][1]);
                    rings.push(arr);
                }


                // if (rings.length = 1) {
                //     view.graphics.removeAll();
                //     var polygon = {
                //         type: "polygon", // autocasts as Polyline
                //         rings: rings,
                //         spatialReference: view.spatialReference
                //     };

                //     var graphic = new Graphic({
                //         geometry: polygon,
                //         symbol: fillSymbol
                //     });
                //     view.graphics.add(graphic);
                // }

                if (rings.length > 2) {
                    view.graphics.removeAll();
                    var gon = {
                        type: geoType, // autocasts as Polyline
                        rings: rings,
                        spatialReference: view.spatialReference
                    };

                    var graphic = new Graphic({
                        geometry: gon,
                        symbol: fillSymbol
                    });
                    view.graphics.add(graphic);
                }
            }
        });
    </script>
</head>

<body>
    <div id="viewDiv">
        <button class="drawPolygon">绘制多边形</button>
    </div>

</body>

</html>
<!-- //DESKTOP-9DFAUNS:1521/orcl localhost //localhost:1521/orcl -->